//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef _PORT_FORWARD_TAB_YG_H__
#define _PORT_FORWARD_TAB_YG_H__

#include "def.h"
#include "logctrl.h"
#include "searchimpl.h"

class CPortForwardTab: public wxPanel, public CSearchImpl<CPortForwardTab>, public IGUIBind, public ITabedPageUI
{
public:
								CPortForwardTab(wxWindow* pParent);
	virtual						~CPortForwardTab(void);

	//IGUIBind
	virtual void 				SetVal(unsigned int nVal);
	virtual void				Reset();

	//ISearchProvider
	virtual void 				GetSearchFields(SEARCHFIELD*& sf, size_t& sz);
	virtual cSearchOwnerID		GetID()				{ return cSearchOwnerIDPortForwarding; }
	virtual void				OnFilterChanged();

	//List Control
	bool						OnDropText(wxCoord x, wxCoord y, const wxString& data) { return false; }
	void						OnDragDropFinished( wxWindowID id, wxDragResult result) {}

	void						OnContextMenu(long id, long col);

	long						GetItemCount(wxWindowID id=wxID_ANY)
	{
		return m_storage.size();
	}

	wxString					GetItemText(long item, long column, wxWindowID id=wxID_ANY);

	int							GetItemColumnImage(long item, long column, wxWindowID id=wxID_ANY);

	virtual int					GetItemImage(long item, wxWindowID id=wxID_ANY)
	{
		return -1;
	}

	wxListItemAttr*		GetItemAttr(long item, wxWindowID id=wxID_ANY);

	virtual cTabPageID			GetTabID() { return cTabIDPortForward; }

protected:
	void						ProcessData();
	void						DoFilter();
	void						DoSort();


	wxString					FieldToString( int column, const PORTFORWARDRULE& entry);

	struct CSortHelper
	{
		CSortHelper(int col, bool bSortAsc):m_col(col), m_bSortAsc(bSortAsc) {}

		bool operator() (const PORTFORWARDRULE& a, const PORTFORWARDRULE& b);

	private:
		int			 m_col;
		bool		 m_bSortAsc;
	};

	wxString					FormatProto(cPortFrorwardProto proto);
	bool						ValidateRule(const PORTFORWARDRULE& rule, bool bNew=true);
	void						LoadData();

protected:
	void						OnRuleAdd(wxCommandEvent& ev);
	void						OnRuleEdit(wxCommandEvent& ev);
	void						OnRuleRemove(wxCommandEvent& ev);
	void						OnRuleAddUpdateUI(wxUpdateUIEvent& ev);
	void						OnRuleEditUpdateUI(wxUpdateUIEvent& ev);
	void						OnRuleRemoveUpdateUI(wxUpdateUIEvent& ev);
	void						OnCBAuto(wxCommandEvent& ev);
	void						OnColumn(wxListEvent& ev);

	void						OnToggle(wxCommandEvent& ev);
	void						OnRuleToggleUpdateUI(wxUpdateUIEvent& ev);

	void						OnPortTest(wxCommandEvent& ev);
	void						OnPortForwardingUpdateUI(wxUpdateUIEvent& ev);

	void						OnPopupClick(wxCommandEvent &evt);

	DECLARE_EVENT_TABLE()


private:
	class CSearchPanel*			m_pSearch;
	bool						m_bSortAsc;
	class wxListCtrl*			m_pList;
	class wxCheckBox*			m_pAuto;
	PORTFORWARDLIST				m_storage;
	PORTFORWARDLIST				m_entriesBeforeFilter;
	unsigned int				m_nSortCol;
	mutable wxListItemAttr		m_attrRed;
	class wxImageList*			m_pImageList;
};

struct CPortForwardRuleEditDlg: public wxDialog
{
								CPortForwardRuleEditDlg(wxWindow* parent, PORTFORWARDRULE& rule, const PORTFORWARDLIST& list,  bool bAdd=true);
	void						TransferData();

protected:
	void						OnOKUpdateUI(wxUpdateUIEvent& ev);
	void						OnPortRange(wxCommandEvent& evt);
	DECLARE_EVENT_TABLE()

private:
	PORTFORWARDRULE&			m_rule;
	const PORTFORWARDLIST&		m_list;
	bool						m_bAdd;

	class wxTextCtrl*			m_pName;
	class wxTextCtrl*			m_pFromPort;
	class wxTextCtrl*			m_pFromPortTo;
	class wxTextCtrl*			m_pIP;
	class wxTextCtrl*			m_pToPort;
	class wxComboBox*			m_pProtoCombo;

	class wxRadioButton*		m_prdioSingle;
	class wxRadioButton*		m_prdioRange;

	class wxStaticText*			m_plblTo;
	
	//Validators
	wxString					m_strFromPort;
	wxString					m_strFromPortTo;
	wxString					m_strToPort;

	class wxSizer*				m_pPortRangeSizer;
};




#endif //_PORT_FORWARD_TAB_YG_H__